package cn.yangliu.springbatch.config;

import cn.yangliu.springbatch.entity.Role;
import cn.yangliu.springbatch.entity.User;
import cn.yangliu.springbatch.job.RoleProcessor;
import cn.yangliu.springbatch.job.RoleReader;
import cn.yangliu.springbatch.job.RoleWriter;
import cn.yangliu.springbatch.job.UserProcessor;
import cn.yangliu.springbatch.job.UserReader;
import cn.yangliu.springbatch.job.UserWriter;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * the type of springbatch
 *
 * <p>
 * .
 *
 * @author 杨柳
 * @date 2021-07-21
 */
@Configuration
public class CommBatchConfig {


    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private UserReader userReader;
    @Autowired
    private UserProcessor userProcessor;
    @Autowired
    private UserWriter userWriter;

    @Autowired
    private RoleReader roleReader;
    @Autowired
    private RoleProcessor roleProcessor;
    @Autowired
    private RoleWriter roleWriter;


    @Bean
    public Step handleUserStep() {
        return stepBuilderFactory.get("getUserData").
                // <输入,输出> 。chunk通俗的讲类似于SQL的commit; 这里表示处理(processor)100条后写入(writer)一次。
                        <User, User>chunk(100).
                //捕捉到异常就重试,重试100次还是异常,JOB就停止并标志失败
                        faultTolerant()
                .retryLimit(3)
                .retry(Exception.class)
                .skipLimit(100)
                .skip(Exception.class)
                //指定ItemReader
                .reader(userReader)
                //指定ItemProcessor
                .processor(userProcessor)
                .writer(userWriter)
                //指定ItemWriter
                .build();

    }

    @Bean
    public Step handleRoleStep() {
        return stepBuilderFactory.get("getRoleData").
                // <输入,输出> 。chunk通俗的讲类似于SQL的commit; 这里表示处理(processor)100条后写入(writer)一次。
                        <Role, Role>chunk(100).
                //捕捉到异常就重试,重试100次还是异常,JOB就停止并标志失败
                        faultTolerant()
                .retryLimit(3)
                .retry(Exception.class)
                .skipLimit(100)
                .skip(Exception.class)
                //指定ItemReader
                .reader(roleReader)
                //指定ItemProcessor
                .processor(roleProcessor)
                .writer(roleWriter)
                //指定ItemWriter
                .build();

    }

}
